import HttpRequest from './request'
import { BASE_URL, TIME_OUT } from './config'
import { sessionCache } from '@/utils/cache'

const request = new HttpRequest({
  baseURL: BASE_URL,
  timeout: TIME_OUT,
  interceptors: {
    requestInterceptor: config => {
      if (sessionCache.getCache('userInfo')) {
        const token = sessionCache.getCache('userInfo').token
        if (token && config.headers) {
          config.headers.Authorization = `Bearer ${token}`
        }
      }

      return config
    },
    requestInterceptorCatch: err => {
      return err
    },
    responseInterceptor: res => {
      return res
    },
    responseInterceptorCatch: err => {
      return Promise.reject(err)
    },
  },
})

export default request
